package sjtu.ist.warehouse4ws.dao.cust;

import java.util.List;

import org.hibernate.SQLQuery;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sjtu.ist.warehouse4ws.dao.BaseDaoHibernate;
import sjtu.ist.warehouse4ws.model.CustDomainmodelstate;
import sjtu.ist.warehouse4ws.model.Domainmodelstate;

public class CustDomainModelStateDaoHibernate extends BaseDaoHibernate<CustDomainmodelstate> implements CustDomainModelStateDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<CustDomainmodelstate> findAllCustDomainModelStates(
			String custDomainModelId) {
		// TODO Auto-generated method stub
		return (List<CustDomainmodelstate>)getHibernateTemplate().find("from CustDomainmodelstate d where d.domainModelid='"+custDomainModelId+"'");
		}

	@Override
	public void savaCustDomainmodelstate(
			CustDomainmodelstate custDomainmodelstate) {
		// TODO Auto-generated method stub
		getHibernateTemplate().setFlushMode(HibernateTemplate.FLUSH_EAGER); 
		getHibernateTemplate().save("CustDomainmodelstate",custDomainmodelstate);
		
	}

	@Override
	public void createTable(String custName) {
		// TODO Auto-generated method stub
		String sql="create table "+custName+"_cust_domainmodelstate ("
			+"id varchar(100) NOT NULL,"
			+"enName varchar(200) DEFAULT NULL,"
			+"cnName varchar(200) DEFAULT NULL,"
			+"description varchar(500) DEFAULT NULL,"
			+"domainModelid varchar(100) NOT NULL,"
			+"groupId varchar(100) DEFAULT '',"
			+"versionId int(10) DEFAULT NULL,"
			+"PRIMARY KEY (id)"
		  	+")";
		SQLQuery query=getSession().createSQLQuery(sql);
		query.executeUpdate();
	}

	@Override
	public List<CustDomainmodelstate> getByGroupAndVersion(String groupId,
			String version) {
		// TODO Auto-generated method stub
		SQLQuery query=getSession().createSQLQuery("select CustDomainmodelstate.* from CustDomainmodelstate where CustDomainmodelstate.groupId=? and versionId=?");
		query.setParameter(0, groupId);
		query.setParameter(1, version);
		List list=query.addEntity(CustDomainmodelstate.class).list();
		return list;
	}

}
